#include <iostream>
#include <string>
using namespace std;

struct Node {
	string val;
	Node* left = nullptr;
	Node* right = nullptr;
};
string sp, tgt;
int flag;
Node* H = nullptr;
void getTree(Node*& h)
{
	string cur;
	cin >> cur;
	if (cur == sp) return;
	h = new Node;
	h->val = cur;
	getTree(h->left);
	getTree(h->right);
}

string find(Node* h, Node* p) {
	if (!h) return "";
	string ret;
	if (h->val == tgt) {
		if (!p) return "";
		if (p->left == h && flag == 1) ret = p->right->val;
		if (p->right == h && flag == 0) ret = p->left->val;
	}
	if(ret.empty()) ret = find(h->left, h);
	if(ret.empty()) ret = find(h->right, h);
	return ret;
}

int main()
{
	cin >> sp;
	getTree(H);
	cin >> tgt >> flag;
	string ret = find(H, nullptr);
	if (ret.empty()) cout << "NULL";
	else cout << ret;
	return 0;
}


// # A B # C D # # E # # F # G # H # # E 0